require(['init'], function () {
    $('#enorll_lesson').val(i18next.t('table.auditions'))
    $('#submit_html').val(i18next.t('course.submit'))
 //点击编辑传值
  var formid=0;
  var needPw=true;
  function loadEnrollData(id, callback) {
      ajaxMethod({
          url:'/api/marketing/lead/'+id,
          success: function (data) {
            if(data.ClassInNo!=null){
              $('input[name="ClassInNo"]').val(data.ClassInNo);
              $('input[name="ClassInNo"]').attr("readonly","readonly");
              $('#resetpassword').show();
            }
            $('input[name="name"]').val(data.name);
            $('input[name="id"]').val(data.id);
            $('input[name="number"]').val(data.number);
            $('input[name="staff"]').val(data.staff);
            $('#source').val(sourcetext);
            getname(data)
            ;(typeof callback == 'function') && callback();
          }
      })
  }

//点击添加
function getname(data){
  if(data.name==null||data.number==null||data.ClassInNo==null){
    $('#enorll_lesson').attr("disabled",true);
  }
  $('#enorll_lesson').on('click',function(){
   var ClassInNo=$('input[name="ClassInNo"]').val();
   var $dom=$('<div class="formList">\
    <div class="formLi">\
         <div class="formTitle required">\
             <span>'+i18next.t('table.name')+'</span>\
         </div>\
         <div class="formContent">\
             <input class="form-control" data-formcheck="length" type="text" name="enroll_name"  value='+data.name+' readonly="readonly">\
         </div>\
     </div>\
     <div class="formLi">\
          <div class="formTitle required">\
              <span>'+i18next.t('info.classinaccountnumber')+'</span>\
          </div>\
          <div class="formContent">\
              <input class="form-control" data-formcheck="length" type="text" name="ClassIn"  value='+(ClassInNo==""?"请填写ClassIn账号":ClassInNo)+' readonly="readonly">\
          </div>\
      </div> \
    <div class="formLi">\
           <div class="formTitle required">\
               <span>'+i18next.t('table.phone')+'</span>\
           </div>\
           <div class="formContent">\
               <input class="form-control" data-formcheck="length" type="text" name="enroll_number"  value='+data.number+' readonly="readonly">\
           </div>\
       </div></div>')
   var popUpBox=new PopUpBox({
     type:'dom',
     style:'default contentScroll',
     submitBtnType:'alert',
     titleText:i18next.t('popupbox.aboutclass'),
     drag:true,
     show:true,
     content: $dom,
     callback:{
       yes: function (e) {
           var chinesename=$('input[name="enroll_name"]').val();
           var mobile=$('input[name="enroll_number"]').val();
           var ClassInNo=$('input[name="ClassIn"]').val();
         ajaxMethod({
           url: '/api/demoment',
           method: 'post',
           data:{
            mobile:mobile,
            chinesename:chinesename,
            ClassInNo:ClassInNo
           },
           success: function (data,status) {
             if(status.code!=0){
               _Tips(status.message)
             }else{
              formid=data.formuid;
             }

           }

         })

       }
     },
     position:{
       width:474,
       height:265
     }
   })
   $('.submitBtnBox').find('.btn').css('margin-left','218px')
  })
}


function bindSubmit(){
  $("#submit_html").on('click',function(){
    if(FormCheck.childrenInputCheck($('#content_maketing'))){
      var id=$('input[name="id"]').val();
      var name=$('input[name="name"]').val();
      var number=$('input[name="number"]').val();
      var staff=$('input[name="staff"]').val();
      var ClassInNo=$('input[name="ClassInNo"]').val();
      var source=$('#source').find("option:selected").attr("data-uid");
      var arr={
          id:id,
          name:name,
          number:number,
          staff:staff,
          ClassInNo:ClassInNo,
          source:source,
          schedule:formid
        }
      if (editFlag) {
        ajaxMethod({
            url: '/api/marketing/lead/'+id,
            type: 'put',
            data: arr,
            success: function (data,status) {
              if(status.code!=0){
                _Tips(status.message)
              }else{
                location.replace("potential_guest_list.html");
              }

            }
        });
      }
    }
  })
}
//检测
var registerDefaultPwd = "";
ajaxMethod({
    url: '/api/organization/1',
    type: 'get',
    success: function (data) {
        registerDefaultPwd = data.registerDefaultPwd;
    }
})
$('[name="ClassInNo"]').on('blur', function () {
    classIntest(true);
})
var classin_nov = "";
function classIntest(cl) {
    classin_nov = $('[name="ClassInNo"]').val();
    ajaxMethod({
        url: '/api/eeo/check',
        method: 'post',
        data: {account: classin_nov},
        success: function (data) {
            if (data == "true") {
                $('.pw').hide();
                $('#prompt').show();
                $('#noprompt').hide();
                $("#resetpassword").attr("disabled", false);
                needPw = false;
            } else if (data == "fasle" || $('input[name="ClassInNo"]').val() != "") {
                $('.pw').show();
                $('#noprompt').show();
                $('[name="pw"]').val(registerDefaultPwd);
                $('#prompt').hide();
                needPw = true;
                $("#resetpassword").attr("disabled", true);
            }
        }

    })
  }
  $('[name="pw"]').on('blur', function () {
      if ($('[name="pw"]').val() != "") {
          $('#prompt').hide();
          $('#noprompt').hide();
      } else {
          $('#noprompt').show();
      }
  })
  //重置密码
  var account = "";
  $("#resetpassword").on('click', function () {
      account = $('[name="ClassInNo"]').val();
      //console.log(account)
      reset()
  })
  function reset() {
      var $ClassInDom = $('<div class="formList" id="J_teacheradd_wrap" style="margin-top:12px;">\
        <div class="formLi" >\
          <div class="formLi" >\
            <div class="formTitle required">\
                <span>'+i18next.t('reset.newpassword')+'</span>\
            </div>\
            <div class="formContent">\
                <input class="form-control" data-formcheck="length" type="text"  name="passwordnew" style="width:226px;">\
            </div>\
        </div>\
        <div class="formLi" >\
            <div class="formTitle required">\
               <span>'+i18next.t('reset.confirmnewpassword')+'</span>\
            </div>\
            <div class="formContent">\
                <input class="form-control" data-formcheck="length" type="text"  name="passwordold" style="width:226px;">\
                <p style="color:red;display:none;margin-top:30px;" class="p">'+i18next.t('reset.twice')+'</p>\
            </div>\
          </div>\
        </div>');
      var popUpBox = new PopUpBox({
          type: 'dom',
          style: 'default contentScroll',
          //submitBtnType:false,
          submitBtnType: 'alert',
          titleText: i18next.t('reset.set'),//'请输入要设置的密码',
          show: true,
          content: $ClassInDom,
          callback: {
              yes: function (e) {
                  var passwordold = $ClassInDom.find('[name="passwordold"]').val();
                  var passwordnew = $ClassInDom.find('[name="passwordnew"]').val();
                  if (passwordold != "" || passwordnew != "") {
                      if (passwordold != passwordnew) {
                          $('.p').show();
                          return false;
                      } else {
                          $('.p').hide();
                          ajaxMethod({
                              url: '/api/eeo/reset',
                              method: 'post',
                              data: {
                                  account: account,
                                  password: passwordold
                              },
                              success: function (data,status) {
                                if(status.code!=0){
                                  _Tips(status.message)
                                }else{
                                  window.location.reaload();
                                }
                              }
                          })
                      }
                  } else {
                      return false;
                  }

              }
          },
          position: {
              width: 480,
              height: 304,
          }
      })
      popUpBox.submitBtnBox.$dom.css('text-align', 'right')
  }


var id = location.search.urlToObj().id;
var sourcetext = location.search.urlToObj().sourcetext;
    var editFlag = false
    if (id) {
        editFlag = true
        loadEnrollData(id, bindSubmit)
        return
    }
    bindSubmit();


})
